/*
 * Copyright (c) 2016 zhangcm. All Rights Reserved.
 */
package com.jnzc.ocr.rest.app.shop.controller.common;

import com.google.common.collect.Maps;
import com.jnzc.ocr.common.utils.BusinessUtils;
import com.jnzc.ocr.common.utils.PasswordUtils;
import com.jnzc.ocr.common.utils.TemplateUtils;
import com.jnzc.ocr.common.utils.ValidateUtils;
import com.jnzc.ocr.common.vo.UserVo;
import com.jnzc.ocr.dependencies.rest.base.BaseController;
import com.jnzc.ocr.rest.app.shop.model.common.CommonUserLoginResultModel;
import com.jnzc.ocr.serv.system.api.SysTemplateApi;
import com.jnzc.ocr.serv.system.consts.SysTemplateConsts;
import com.jnzc.ocr.serv.system.consts.SysUserConsts;
import com.jnzc.ocr.serv.system.entity.SysTemplate;
import com.jnzc.ocr.serv.system.entity.SysUser;
import com.jnzc.ocr.serv.third.api.CaptchaApi;
import com.jnzc.ocr.serv.third.api.SmsApi;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author zhangcm
 * @version 1.0, 2017/9/3
 */
@Api(tags = {"common - 验证码接口"})
@RestController
@RequestMapping("/common/captcha")
public class CommonCaptchaController extends BaseController {

    @Autowired
    private CaptchaApi captchaApi;

    @Autowired
    private SysTemplateApi sysTemplateApi;

    @Autowired
    private SmsApi smsApi;

    @ApiOperation(value = "发送验证码短信")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile", value = "手机号", required = true, paramType = "form")
    })
    @PostMapping("/sendSms")
    public void sendSms(@RequestParam String mobile){
        ValidateUtils.isMobile(mobile);
        // 生成验证码
        String captcha = captchaApi.create(mobile);
        // 发送短信
        Map<String, String> paramMap = Maps.newHashMap();
        paramMap.put("code", captcha);
        smsApi.send(mobile, SysTemplateConsts.TEMPLATE_CODE_CAPTCHA, paramMap);
    }
}
